package com.wanpinghui.supplier.business.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TimeUtil;

import java.util.Calendar;
import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/1/26
 ***/

public class PersonalCircleAdapter extends BaseRecyclerViewAdapter<DynamicBean,PersonalCircleAdapter.CircleHolder>{
    private boolean isMyCircle;
    private OnOperaListener operaListener;
    public PersonalCircleAdapter(Context context) {
        super(context);
    }

    @Override
    public void setDatas(List<DynamicBean> list) {
        this.datas = list;
        if (isMyCircle) {
            DynamicBean dynamicBean = new DynamicBean();
            dynamicBean.setCreated_at(System.currentTimeMillis()/1000);
            datas.add(0,dynamicBean);
        }
        notifyDataSetChanged();
    }

    public void setOperaListener(OnOperaListener listener) {
        this.operaListener = listener;
    }

    @Override
    public CircleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_personal_circle, null);
        return new CircleHolder(view);
    }

    public void setMyCircle(boolean myCircle) {
        isMyCircle = myCircle;
    }

    @Override
    public void onBindViewHolder(CircleHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (isMyCircle && position == 0) {
            holder.layoutAdd.setVisibility(View.VISIBLE);
            holder.layoutAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operaListener != null) {
                        operaListener.onPublish();
                    }
                }
            });
            holder.textViewTime.setText("今天");
            holder.textViewYear.setVisibility(View.GONE);
            holder.textViewContent.setVisibility(View.GONE);
            holder.layoutMultiImg.setVisibility(View.GONE);
        }else {
            holder.layoutAdd.setVisibility(View.GONE);
            final DynamicBean dynamicBean = datas.get(position);
            //设置跨年年份
            if (position == 0) {
                if (WTimeUtil.getYear(dynamicBean.getCreated_at()) != WTimeUtil.getYear(System.currentTimeMillis() / 1000)) {
                    holder.textViewYear.setVisibility(View.VISIBLE);
                    holder.textViewYear.setText(WTimeUtil.getYear(dynamicBean.getCreated_at()) + "年");
                } else {
                    holder.textViewYear.setVisibility(View.GONE);
                }
            } else {
                if (WTimeUtil.getYear(dynamicBean.getCreated_at()) != WTimeUtil.getYear(datas.get(position - 1).getCreated_at())) {
                    holder.textViewYear.setVisibility(View.VISIBLE);
                    holder.textViewYear.setText(WTimeUtil.getYear(dynamicBean.getCreated_at()) + "年");
                } else {
                    holder.textViewYear.setVisibility(View.GONE);
                }
            }

            //设置添加动态按钮
//            if (position == 0 && isMyCircle) {
//                holder.layoutAdd.setVisibility(View.VISIBLE);
//            } else {
//                holder.layoutAdd.setVisibility(View.GONE);
//            }

            //设置时间
            if (position == 0) {
                holder.textViewTime.setVisibility(View.VISIBLE);
                holder.textViewTime.setText(getTextTime(dynamicBean.getCreated_at()));
            } else {
                if (TimeUtil.getBetweenDay(dynamicBean.getCreated_at() * 1000, datas.get(position - 1).getCreated_at() * 1000) == 0) {
                    holder.textViewTime.setVisibility(View.INVISIBLE);
                } else {
                    holder.textViewTime.setVisibility(View.VISIBLE);
                    holder.textViewTime.setText(getTextTime(dynamicBean.getCreated_at()));
                }
            }

            //设置内容
            if (dynamicBean.getContentImgs() != null && dynamicBean.getContentImgs().size() > 0) {
                holder.textViewContent.setVisibility(View.GONE);
                holder.layoutMultiImg.setVisibility(View.VISIBLE);
                holder.textViewContent2.setText(dynamicBean.getContent());
                if (dynamicBean.getContentImgs().size() == 1) {
                    holder.layoutImage.setVisibility(View.GONE);
                    holder.imageView4.setVisibility(View.GONE);
                    holder.imageView1.setVisibility(View.VISIBLE);
                    if (dynamicBean.getContentImgs().get(0).getType() == 1) {
                        Glide.with(context).load(dynamicBean.getContentImgs().get(0).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView1);
                    } else {
                        Glide.with(context).load(dynamicBean.getContentImgs().get(0).getThumb_url()).error(R.mipmap.loading_140x140).into(holder.imageView1);
                    }
                } else if (dynamicBean.getContentImgs().size() == 2) {
                    holder.layoutImage.setVisibility(View.VISIBLE);
                    holder.imageView2.setVisibility(View.VISIBLE);
                    holder.imageView1.setVisibility(View.VISIBLE);
                    holder.imageView3.setVisibility(View.GONE);
                    holder.imageView4.setVisibility(View.GONE);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(0).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView1);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(1).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView2);
                } else if (dynamicBean.getContentImgs().size() == 3) {
                    holder.layoutImage.setVisibility(View.VISIBLE);
                    holder.imageView2.setVisibility(View.VISIBLE);
                    holder.imageView1.setVisibility(View.VISIBLE);
                    holder.imageView3.setVisibility(View.VISIBLE);
                    holder.imageView4.setVisibility(View.GONE);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(0).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView1);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(1).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView2);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(2).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView3);
                } else {
                    holder.layoutImage.setVisibility(View.VISIBLE);
                    holder.imageView2.setVisibility(View.VISIBLE);
                    holder.imageView1.setVisibility(View.VISIBLE);
                    holder.imageView3.setVisibility(View.VISIBLE);
                    holder.imageView4.setVisibility(View.VISIBLE);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(0).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView1);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(1).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView2);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(2).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView3);
                    Glide.with(context).load(dynamicBean.getContentImgs().get(3).getThumbnailUrl()).error(R.mipmap.loading_140x140).into(holder.imageView4);
                }
                if (dynamicBean.getContentImgs().size() > 1) {
                    holder.textViewCount.setVisibility(View.VISIBLE);
                    holder.textViewCount.setText("共" + dynamicBean.getContentImgs().size() + "张");
                } else {
                    holder.textViewCount.setVisibility(View.GONE);
                }
            } else {
                holder.textViewContent.setVisibility(View.VISIBLE);
                //holder.textViewContent2.setVisibility(View.GONE);
                holder.layoutMultiImg.setVisibility(View.GONE);
                holder.textViewContent.setText(dynamicBean.getContent());
            }

            holder.textViewContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operaListener != null) {
                        operaListener.onContentClick(dynamicBean, true);
                    }
                }
            });
            holder.layoutMultiImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (operaListener != null) {
                        operaListener.onContentClick(dynamicBean, false);
                    }
                }
            });
        }

    }

    public void changeData(DynamicBean mCurrentDynamic) {
        if (mCurrentDynamic != null && getItemCount() > 0) {
            for (int i = 0; i<datas.size();i++) {
                if (datas.get(i).getId() == mCurrentDynamic.getId()) {
                    datas.set(i,mCurrentDynamic);
                    notifyItemChanged(i);
                }
            }
        }
    }

    private SpannableString getTextTime(long created_at) {
        if (TimeUtil.getBetweenDay(created_at*1000,System.currentTimeMillis()) == 0) {
            return new SpannableString("今天");
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(created_at * 1000);
            int month = calendar.get(Calendar.MONTH)+1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            String dayStr = day < 10 ? ("0"+day):(day+"");
            SpannableString string = new SpannableString(dayStr+month+"月");
            string.setSpan(new RelativeSizeSpan(0.5f),2,string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            return string;
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class CircleHolder extends RecyclerView.ViewHolder{
        TextView textViewTime;
        TextView textViewContent;
        TextView textViewContent2;
        ImageView imageView1;
        ImageView imageView2;
        ImageView imageView3;
        ImageView imageView4;
        LinearLayout layoutImage;
        TextView textViewCount;
        TextView textViewYear;
        ImageView videoTag;
        RelativeLayout layoutAdd;
        RelativeLayout layoutMultiImg;
        public CircleHolder(View itemView) {
            super(itemView);
            textViewTime = (TextView) itemView.findViewById(R.id.tv_time);
            textViewContent = (TextView) itemView.findViewById(R.id.tv_content1);
            textViewContent2 = (TextView) itemView.findViewById(R.id.tv_content2);
            imageView1 = (ImageView) itemView.findViewById(R.id.iv_image1);
            imageView2 = (ImageView) itemView.findViewById(R.id.iv_image2);
            imageView3 = (ImageView) itemView.findViewById(R.id.iv_image3);
            imageView4 = (ImageView) itemView.findViewById(R.id.iv_image4);
            layoutImage = (LinearLayout) itemView.findViewById(R.id.ll_image2);
            textViewCount = (TextView) itemView.findViewById(R.id.tv_image_count);
            textViewYear = (TextView) itemView.findViewById(R.id.tv_year);
            //videoTag = (ImageView) itemView.findViewById(R.id.)
            layoutAdd = (RelativeLayout) itemView.findViewById(R.id.rl_add);
            layoutMultiImg = (RelativeLayout) itemView.findViewById(R.id.rl_multi_img);
            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface OnOperaListener {
        void onContentClick(DynamicBean dynamicBean, boolean onlyText);
        void onPublish();
    }
}
